package com.bookm.bookm.service.impl;

import cn.hutool.db.Db;
import cn.hutool.db.handler.BeanListHandler;
import com.bookm.bookm.entity.*;
import com.bookm.bookm.service.LogmaintenanceService;
import com.bookm.bookm.utils.ResultUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LogmaintenanceServiceImpl implements LogmaintenanceService {

    @Override
    public Result getList() {
        //new一个新数组
        List<Logmaintenance> rst= new ArrayList<>();
        try {
            //用SQL语句来调用图书表单
            List<BookingLog> bookingList = Db.use().query("select name,reader.id,book_id,rd_date from booking,reader where booking.id=reader.id order by booking.rd_date desc;", BeanListHandler.create(BookingLog.class));
            //用SQL语句来调用读者表单
            List<RecorderLog> recorderList = Db.use().query("select name,reader.id,book_id,rd_date from recorder,reader where recorder.id=reader.id order by recorder.rd_date desc", BeanListHandler.create(RecorderLog.class));
            //将图书表单的数据放进数组中
            for (BookingLog booking : bookingList) {
                Logmaintenance logmaintenance = new Logmaintenance();
                //读者名字
                logmaintenance.setName(booking.getName());
                //数据进库时间
                logmaintenance.setRd_date(booking.getRdDate());
                //数据类型（借阅）
                logmaintenance.setType(0);
                logmaintenance.setData(booking);
                rst.add(logmaintenance);
            }
            for (RecorderLog booking : recorderList) {
                Logmaintenance logmaintenance = new Logmaintenance();
                //读者名字
                logmaintenance.setName(booking.getName());
                //数据进库时间
                logmaintenance.setRd_date(booking.getRdDate());
                //数据类型（预定）
                logmaintenance.setType(1);
                logmaintenance.setData(booking);
                rst.add(logmaintenance);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        //判断能否正常运行
        if(rst == null){
            return ResultUtil.failure();
        }
        //返回从数据库中拿到的数据
        return ResultUtil.success(rst);
    }
}
